package cn.wecloud.im.multiplayervideodemo.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.text.TextUtils;

import cn.wecloud.im.multiplayervideodemo.App;


public class AppSharePre {

    public static void setClientId(String clientID) {
        SharedPreferences sp = getSharePreferencesInstance();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("CLIENT_ID", clientID);
        editor.commit();
    }

    public static String getClientId() {
        SharedPreferences sp = getSharePreferencesInstance();
        return sp.getString("CLIENT_ID", "");
    }

    public static void setToken(String token) {
        SharedPreferences sp = getSharePreferencesInstance();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("TOKEN", token);
        editor.commit();
    }

    public static String getToken() {
        SharedPreferences sp = getSharePreferencesInstance();
        return sp.getString("TOKEN", "");
    }

    //获取用户电话号码以及 是否修改版本号
    public static String getPhoneData() {
        SharedPreferences sp = getSharePreferencesInstance();
        String clientId = getClientId();
        if (!TextUtils.isEmpty(clientId)) {
            String infoString = sp.getString(clientId, "");
            if (TextUtils.isEmpty(infoString)) {
                return null;
            } else {
                return infoString;
            }
        } else {
            return null;
        }
    }

    //储存用户电话号码 以及 版本号
    public static void setPhoneData(String json) {
        SharedPreferences sp = getSharePreferencesInstance(); // 获得Preferences
        SharedPreferences.Editor editor = sp.edit(); // 获得Editor
        String clientId = getClientId();
        if (TextUtils.isEmpty(clientId)) {
            return;
        }
        editor.putString(clientId, json); // 将密码存入Preferences
        editor.commit();
    }



    public static void setStartPermission(boolean isStartPermission) {
        SharedPreferences sp = getSharePreferencesInstance();
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("start_permission", isStartPermission);
        editor.commit();
    }

    public static boolean isStartPermission() {
        SharedPreferences sp = getSharePreferencesInstance();
        return sp.getBoolean("start_permission", false);
    }

    public static void setIgnoringBattery(boolean ignoringBattery) {
        SharedPreferences sp = getSharePreferencesInstance();
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("ignoring_battery", ignoringBattery);
        editor.commit();
    }

    public static boolean isIgnoringBattery() {
        SharedPreferences sp = getSharePreferencesInstance();
        return sp.getBoolean("ignoring_battery", false);
    }

    /**
     * 获取sharePreference的对象
     */
    private static SharedPreferences getSharePreferencesInstance() {
        SharedPreferences sp = App.instance.getSharedPreferences("cn.wecloud.chatkit", Activity.MODE_PRIVATE);
        return sp;
    }

}
